﻿/*
==============================================================================
// 出库单明细管理 页面控制层代码。
==============================================================================
*/
(function ($) {

    if (!window.platform) { window.platform = new Object(); }
    if (!window.platform.RequisitionBillDetailManage) { window.platform.RequisitionBillDetailManage = new Object(); }

    window.platform.RequisitionBillDetailManage.initPage = function (RequisitionBillID, createFunction, editFunction, removeFunction) {

        var _bindQueryButtonEvent = function () {
            $("#aquery", "#queryFm").bind("click", function () {
                var fm = $('#queryFm').serialize();
                $('#dg').datagrid('load', { RequisitionBillID: RequisitionBillID, model: fm });
            });
        };

        var _bindToolBarButtonEvent = function () {
            $("#acreate", "#toolbar").bind("click", function () {
                _createRequisitionBillDetail(RequisitionBillID);
            });
            $("#aedit", "#toolbar").bind("click", function () {
                _editRequisitionBillDetail(RequisitionBillID);
            });
            $("#aremove", "#toolbar").bind("click", function () {
                _singleAction();
            });

            $("#areload", "#toolbar").bind("click", function () {
                _reloadRequisitionBillDetail();
            });
            $("#asave", "#tb").bind("click", function () {

                _saveRequisitionBillDetail($('#dd'));

            });
            $("#aclose", "#tb").bind("click", function () {

                $('#dd').dialog('close');


            });
        };

        var _RequisitionBillDetailDataGridInit = function () {
            var options = {
                singleSelect: true,
                checkOnSelect: false,
                selectOnCheck: false,
                fit: true,
                border: false,
                rownumbers: true,
                nowrap: true,
                striped: true,
                remoteSort: false,
                url: "/RequisitionDetailManage/RequisitionBillDetailJson",
                queryParams: { RequisitionBillID: RequisitionBillID, model: $('#queryFm').serialize() },
                idField: "ID",
                frozenColumns: [[
                    { field: 'RequisitionBillName', title: '出库单名称', width: 150, align: 'center', sortable: true },
                    { field: 'GoodsClassName', title: '物品类别名称', width: 150, align: 'center', sortable: true },

                      { field: 'Name', title: '物品名称', width: 150, align: 'center', sortable: true },

                ]],
                columns: [[
                   {
                       field: 'Unit', title: '单位', width: 100, align: 'center', sortable: true
                   },
                    {
                        field: 'Quantity', title: '数量', width: 100, align: 'center', sortable: true
                    },
                     {
                         field: 'Price', title: '单价', width: 100, align: 'center', sortable: true
                     },
                      {
                          field: 'AfterTaxPrice', title: '税后单价', width: 100, align: 'center', sortable: true
                      },

                   { field: 'Remark', title: '备注', width: 250, align: 'center' },

                ]],
                pagination: true,

                toolbar: "#toolbar",
                onDblClickRow: function (rowIndex, rowData) {
                    if (editFunction) {
                        _editRequisitionBillDetail(rowData);
                    }
                }

            };

            $("#dg").datagrid(options);
        };



        _bindQueryButtonEvent();
        _bindToolBarButtonEvent();
        _RequisitionBillDetailDataGridInit();
    };



    _createRequisitionBillDetail = function (RequisitionBillID) {
        $('#dd').dialog({
            title: '新建物品明细',
            width: 680,
            height: 300,
            closed: false,
            cache: false,
            resizable: true,
            href: '/RequisitionDetailManage/CreateEdit?id=0&RequisitionBillID=' + RequisitionBillID,
            modal: true,
            buttons: '#tb'


        });

    };

    _editRequisitionBillDetail = function (row) {
        row = row ? row : $("#dg").datagrid("getSelected");

        if (row) {
            $('#dd').dialog({
                title: '编辑物品明细',
                width: 680,
                height: 300,
                closed: false,
                cache: false,
                resizable: true,
                href: '/RequisitionDetailManage/CreateEdit?RequisitionBillID=' + row.OffRequisitionBillID + '&id=' + row.ID,
                modal: true,
                buttons: '#tb'


            });
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }
    };

    _saveRequisitionBillDetail = function (dialog) {
        var verifyResult = $("form", dialog).form("validate");
        if (!verifyResult) { return false };




        var RequisitionBillDetail = $(dialog).form().serializeObject();
        var _callback = function (result) {
            if (result.success) {
                //$.messager.alert('操作提醒', '操作成功');
                _reloadRequisitionBillDetail();
            } else {
                $.messager.alert('错误提醒', result.msg, 'error');
            }
            $('#dd').dialog('close');
        };

        $.post("/RequisitionDetailManage/Save", RequisitionBillDetail, function (result) {
            _callback.call(this, result);
        }, 'json');
    };

    _reloadRequisitionBillDetail = function () {
        $('#dg').datagrid('clearChecked');
        $('#dg').datagrid('clearSelections');
        $('#dg').datagrid('reload');
    };

    _batchAction = function () {
        var rows = $('#dg').datagrid('getSelected');
        var data = [];
        if (rows.length != 0) {
            for (var i = 0; i < rows.length; i++) {
                data.push(rows[i].ID);
            }
        }

        else {
            $.messager.alert("请选择一行");
        }

        if (data.length != 0) {
            $.messager.confirm('确认', "您确定要删除所选物品吗", function (r) {
                if (r) {
                    $.post("/RequisitionDetailManage/BatchDelete", { ids: data.join(',') }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadRequisitionBillDetail();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }

    };

    _singleAction = function (row) {
        row = row == undefined ? $('#dg').datagrid("getSelected") : row;
        if (row) {
            $.messager.confirm('确认', "您确定要删除该物品明细吗", function (r) {
                if (r) {
                    $.post("/RequisitionDetailManage/SingleDelete", { id: row.ID }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadRequisitionBillDetail();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }
    }


})(jQuery);